#!/usr/bin/python
# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2024/7/17 17:07
# @File           : task
# @IDE            : PyCharm
# @desc           : 描述
from datetime import date, datetime
from typing import List

from sqlalchemy import Boolean, Date, String, JSON, Integer, DateTime, Float, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship

from apps.dispatch.assignment.models import DCTaskAssignUsers
from apps.dispatch.feedback.models import DCTaskFeedback
from apps.dispatch.need.models import DCTaskNeed
from apps.dispatch.team.models import DCTaskWorkTeam

from db.db_base import BaseModel, AuditModel


class DCTask(AuditModel):
    __tablename__ = "dc_dispatch_task"
    __table_args__ = ({"comment": "生产任务池"})

    ticket_id: Mapped[str] = mapped_column(String(50), index=True, nullable=False, unique=True, comment="工单编号")
    ao_id: Mapped[str] = mapped_column(String(50), index=True, nullable=False, comment="AO编号")
    ao_name: Mapped[str] = mapped_column(String(100), comment="AO名称")
    ao_type: Mapped[str] = mapped_column(String(50), comment="AO类型，AAO、DISR、FRR、AO等")

    product_type: Mapped[str] = mapped_column(String(50), comment="产品类型，机型")
    flight: Mapped[str] = mapped_column(String(50), comment="架次或制造序列号")
    station: Mapped[str] = mapped_column(String(50), comment="工位")
    position: Mapped[str] = mapped_column(String(50), nullable=True, comment="站位")
    sub_station: Mapped[str] = mapped_column(String(50), nullable=True, comment="二级工位")
    team: Mapped[str] = mapped_column(String(50), nullable=True, comment="班组")
    team_leader: Mapped[str] = mapped_column(String(50), nullable=True, comment="班组长")

    group1: Mapped[str] = mapped_column(String(50), nullable=True, comment="分组1")
    group2: Mapped[str] = mapped_column(String(50), nullable=True, comment="分组2")
    tag: Mapped[str] = mapped_column(String(100), nullable=True, comment="标签")

    plan_user: Mapped[str] = mapped_column(String(50), nullable=True, comment="计划生成人员")
    plan_date: Mapped[datetime] = mapped_column(DateTime, nullable=True, comment="计划生成时间")
    plan_start: Mapped[date] = mapped_column(Date, nullable=True, comment="计划开工时间")
    plan_end: Mapped[date] = mapped_column(Date, nullable=True, comment="计划完工时间")

    start: Mapped[datetime] = mapped_column(DateTime, nullable=True, comment="实际开工时间")
    end: Mapped[datetime] = mapped_column(DateTime, nullable=True, comment="实际完工时间")
    
    priority: Mapped[int] = mapped_column(Integer, default=0, comment="AO生产优先级")
    order: Mapped[int] = mapped_column(Integer, nullable=True, comment="AO展示排序")

    status: Mapped[str] = mapped_column(String(20), comment="AO状态: 未开始，已完成，完工")

    material_match: Mapped[bool] = mapped_column(Boolean, nullable=True, comment="物料配套")
    delivery_message: Mapped[bool] = mapped_column(Boolean, nullable=True, comment="配送通知")
    pi: Mapped[bool] = mapped_column(Boolean, nullable=True, comment="质保标注")

    feedback: Mapped[List["DCTaskFeedback"]] = relationship(back_populates="task")
    assign_users: Mapped[List["DCTaskAssignUsers"]] = relationship(back_populates='task')
    needs: Mapped[List["DCTaskNeed"]] = relationship(back_populates="task")
    work_teams: Mapped[List["DCTaskWorkTeam"]] = relationship(back_populates="task")

    depends: Mapped[dict] = mapped_column(JSON, nullable=True, default={}, comment="AO的依赖，包括人员数、资质技能、前期AO")

    in_daily_plan: Mapped[bool] = mapped_column(Boolean, nullable=True, comment="日计划")
    in_weekly_plan: Mapped[bool] = mapped_column(Boolean, nullable=True, comment="周计划")
